﻿using System;
using fastCSharp;
using System.Reflection;
using fastCSharp.code.cSharp;
using fastCSharp.net;

namespace diantou.web.include
{
    /// <summary>
    /// 评论视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    /// <typeparam name="commentType"></typeparam>
    /// <typeparam name="valueType"></typeparam>
    abstract class comment<viewType, commentType, valueType, memberCacheType> : currentUserView<viewType>
        where viewType : comment<viewType, commentType, valueType, memberCacheType>
        where commentType : diantou.dataAccess.comment<commentType, valueType, memberCacheType>
        where valueType : class
        where memberCacheType : class
    {
        /// <summary>
        /// 评论列表
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected userComment<commentType, valueType, memberCacheType>[] comments;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="linkId"></param>
        /// <returns></returns>
        protected bool loadView(int linkId)
        {
            commentType[] values = getter(linkId);
            comments = values.getArray(value => new userComment<commentType, valueType, memberCacheType> { Comment = value, UserId = currentUserId });
            return true;
        }

        /// <summary>
        /// 评论集合获取委托
        /// </summary>
        private static readonly Func<int, returnValue<commentType[]>> getter;
        static comment()
        {
            Type type = typeof(diantou.dataAccess.tcpCall);
            type = type.Assembly.GetType(type.FullName + "+" + typeof(commentType).Name);
            getter = (Func<int, returnValue<commentType[]>>)Delegate.CreateDelegate(typeof(Func<int, returnValue<commentType[]>>), type.GetMethod("Comments", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(int) }, null));
        }
    }
}
